package basic.class2;

public class TestDemo {
    public static void main(String[] args) {
        String str = "[(()]}";
        boolean valid = isValid(str);
    }

    private static boolean isValid(String str) {
        int r = 0, n = str.length();
        if (n % 2 != 0) {
            return false;
        }
        char[] stack = new char[n];
        char[] chars = str.toCharArray();
        for (char c : chars) {
            if ('(' == c || '[' == c || '{' == c) {
                stack[r++] = c;
            } else {
                if (r == 0) {
                    return false;
                }
                char k = stack[--r];
                if (k == '(' && c != ')' || k == '[' && c != ']' || k == '{' && c != '}') {
                    return false;
                }
            }
        }
        return r == 0;
    }
}
